﻿@{
    ViewBag.Title = "Reports";
}

<h2>Reports</h2>
<table style="width:100%;">
    <tr>
        <td>Fund:</td>
        <td>
            <select name="fund" id="fund" style="width:300px;">
            @foreach (var fundItem in new Host.Models.Data().Fund)
            { 
                <option value="@fundItem.FundId" style="text-decoration:none;">@fundItem.FundName</option>
            }
            </select>
        </td>
    </tr>
    <tr>
        <td style="width:120px;">Report Type:</td>
        <td>
            <select id="reportType" name="reportType" style="width:300px;">
                    <option value="99">System Exceptions</option>
                    <option value="1">Claims</option>
                    <option value="2">Members</option>
                    <option value="1">Employer</option>
                    <option value="1">Transaction</option>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">Report Details</td>
    </tr>
    <tr>
        <td colspan="2"> <hr style="width:100%; height:1px; color:#000;" /> </td>
    </tr>
    <tr>
        <td colspan="2" >
            <table id="report"  style="width:100%; border:1px solid #0000;">
            <thead>
               
            </thead>
            </table>
        </td>
    </tr>
</table>
<script type="text/javascript">
    $(function () {
        //$("#reportType").selectmenu({});
        //$("#fund").selectmenu({});
        $("#reportType").change(function () {
            BuildReport();
        });
        $("#fund").change(function () { BuildReport(); });
        BuildReport();
        $("#fund").selectmenu({});
        $("#reportType").selectmenu({});
    });

    function BuildReport() {
        var Model = { FileExceptionLog: ko.observableArray([]), add: function () { } };
        Model.FileExceptionLog.push({ SourceId: $("#reportType").val(), FundId: $("#fund").val() });
        var d = ko.toJSON(Model.FileExceptionLog);
        var a = $.parseJSON(d);

        $.ajax({
            url: '/Reports/GetReport', type: "POST",
            data: JSON.stringify(a),
            contentType: 'application/json; charset=utf-8',
            success: function (data) { //Close if successful
                //report
                $("#report").find("tr").remove(); //Clean up the control
                $("#report").find("td").remove(); //Clean up the control
                $("#report").append("<tr style='border:1px solid #0000; background-color:#58809a; color:White;' ><th>Exception /Error</th><th>Date Raised</th></tr>");
                var cnt = 0;
                $.each(data, function () {
                    $.each(this, function (k, v) {
                        $("#report").append("<tr><td>" + v.ExceptionDescription + "</td><td>" + v.RaisedDateTime + "</td></tr>");
                        cnt = cnt + 1;
                    });
                })
                $("#report").append("<tfoot><tr style='border:1px solid #0000; background-color:#58809a; color:White; height:30px;'> <td >Total : " + cnt + " Exceptions/ errors</td><td></td></tr></tfoot>");
            },
            error: function () { } //Close the dialog}
        });
        
     }
</script>
